package com.zhuss.core.banner;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.banner.DetailBannerBo;
import com.zhuss.bean.bo.core.banner.SearchBannerBo;
import com.zhuss.bean.bo.core.banner.wechat.ListByTypeBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.core.banner.DetailBannerVo;
import com.zhuss.bean.vo.core.banner.ListBannerVo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.ReadService;
import com.zhuss.core.processor.BannerRead;
import com.zhuss.exception.WpsException;


/**
 * banner查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020-02-05 15:59
 *
 */
@Component("bannerReadService")
public class BannerReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("bannerRead")
	private BannerRead bannerRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailBannerBo bo = (DetailBannerBo) param;
		Long bannerId = bo.getBannerId();
		DetailBannerVo vo = (DetailBannerVo) bannerRead.detail(bannerId);
		
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKey(vo.getPic());
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		vo.setUrl(downloadVo.getUrl());
		
		return vo;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	@Override
	public Object list(Object param) throws WpsException {
		SearchBannerBo bo = (SearchBannerBo) param;
		Object result = bannerRead.list(bo);
		transfer(result);
		return result;
	}
	
	/**
	 * 小程序
	 * 列表
	 * @throws WpsException 
	 */
	public Object listByType(Object param) throws WpsException {
		ListByTypeBo bo = (ListByTypeBo) param;
		List<DetailBannerVo> result = bannerRead.listByType(bo.getType());
		transferList(result);
		return result;
	}

	private void transferList(List<DetailBannerVo> result) throws WpsException {
		for (DetailBannerVo vo : result) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKey(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrl(downloadVo.getUrl());
			
		}
		
	}

	@SuppressWarnings("unchecked")
	private void transfer(Object result) throws WpsException {
		PageVo<ListBannerVo> pageVo = (PageVo<ListBannerVo>) result;
		for (ListBannerVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKey(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrl(downloadVo.getUrl());
			
		}
	}
	
}

